package com.example.buyticketlishi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.baselibrary.Utils.LogUtil;
import com.example.baselibrary.Utils.SelfToastUtil;
import com.example.baselibrary.Utils.StringUtil;
import com.example.buyticketlishi.R;
import com.example.buyticketlishi.bean.RzfindTimeControlListResultBean;

import java.util.ArrayList;
import java.util.List;

public class TimeSelectItemAdapter extends RecyclerView.Adapter<TimeSelectItemAdapter.ViewHolder> {

    private Context context;
    private List<RzfindTimeControlListResultBean.DataBean> dataBeanList;
    private int number;
    private List<String> itemSelect;
    private IsAllSelectListener isAllSelectListener;

    public TimeSelectItemAdapter(Context context, List<RzfindTimeControlListResultBean.DataBean> dataBeanList, int number) {

        if (itemSelect == null) {
            itemSelect = new ArrayList<>();
            for (int i = 0; i < dataBeanList.size(); i++) {
                itemSelect.add("");
            }
        }
        this.context = context;
        this.dataBeanList = dataBeanList;
        this.number = number;


    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


        View view = LayoutInflater.from(context).inflate(R.layout.adapter_time_select_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.timeSelectTitle.setText(dataBeanList.get(position).getProjectShortName());

        List<RzfindTimeControlListResultBean.DataBean.TimeControlListBean> timeControlList = dataBeanList.get(position).getTimeControlList();
        TimeSelectAdapter timeSelectAdapter = new TimeSelectAdapter(context, timeControlList, number, position, itemSelect);

        // 给recycleView 设置布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false);
        holder.timeSelect.setLayoutManager(linearLayoutManager);
        holder.timeSelect.setAdapter(timeSelectAdapter);

        timeSelectAdapter.setItemSelectListener(new TimeSelectAdapter.ItemSelectListener() {
            @Override
            public void itemData(List<String> itemDate) {
                //判断是否所有的都为空
                for (int i = 0; i < itemDate.size(); i++) {
                    if (itemDate.get(i) == null || itemDate.get(i).equals("")) {
//                        SelfToastUtil.getInstance().show(dataBeanList.get(i).getProjectShortName() + "未选择游玩时段");
                        return;
                    }
                }
                //如果通过的话，那么就做下一步
                isAllSelectListener.isAllsetect(itemDate);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final RecyclerView timeSelect;
        private final TextView timeSelectTitle;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            timeSelect = (RecyclerView) itemView.findViewById(R.id.tv_time_item_show);
            timeSelectTitle = (TextView) itemView.findViewById(R.id.tv_time_item_title);
        }
    }

    public interface IsAllSelectListener {
        void isAllsetect(List<String> Selectdata);
    }

    public void SetIsAllSelectListener(IsAllSelectListener selectListener) {
        isAllSelectListener = selectListener;
    }
}
